﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLKDS.DTO;

namespace QLKDS.DAL
{
    public class CategoryManager
    {
        public CategoryInfo GetCategoryById(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var queryCategory = from d in db.Categories
                                    where d.Id.Equals(id)
                                    select new CategoryInfo { Id = d.Id,  Name = d.Name,Description=d.Description,CreatedBy=d.CreatedBy,CreatedDate=d.CreatedDate,ModifiedBy=d.ModifiedBy,ModifiedDate=d.ModifiedDate};

                return queryCategory.First();
            }
        
        }


        public IList<CategoryInfo> ListCategory()
        {
            using (var db = new QLKDSDataContext())
            {
                var queryCategory = from d in db.Categories
                                   select new CategoryInfo { Id = d.Id,Name = d.Name, Description = d.Description, CreatedBy = d.CreatedBy, CreatedDate = d.CreatedDate, ModifiedBy = d.ModifiedBy, ModifiedDate = d.ModifiedDate};

                return queryCategory.ToList();
            }
        }
    

        public void CreateCategory(Category Category)
        {
            using (var db = new QLKDSDataContext())
            {
                db.Categories.InsertOnSubmit(Category);

                db.SubmitChanges();
               
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Category"></param>
        public void UpdateCategory(Category Category)
        {
            using (var db = new QLKDSDataContext())
            {
                var _Category = db.Categories.Single(d => d.Id.Equals(Category.Id));

                _Category.Name = Category.Name;
                _Category.Description = Category.Description;
                _Category.CreatedBy = Category.CreatedBy;
                _Category.CreatedDate = Category.CreatedDate;
                _Category.ModifiedBy = Category.ModifiedBy;
                _Category.ModifiedDate = Category.ModifiedDate;
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void DeleteCategory(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _Category = db.Categories.Single(d => d.Id.Equals(id));

                db.Categories.DeleteOnSubmit(_Category);
                db.SubmitChanges();
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool HasNameExisted(string name)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Categories
                            where d.Name.Equals(name)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="code"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool HasNameDuplicated(string name, long id)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Categories
                            where d.Name.Equals(name)
                            where !d.Id.Equals(id)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

     
    }
}
